﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.IRepositories;
using furion.blog.Domain.MySQL;
using furion.blog.Domain.Shared;
using furion.blog.Domain.Shared.Dto.Email;
using furion.blog.Utils;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System.Text;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 系统数据初始化类
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "InitApi" })]
    [Route("api/init")]
    public class InitApiController
    {
        private readonly IWebHostEnvironment _hostingEnvironment;
        private readonly IGeoRepository _geoRepository;
        private IEmailService _emailService;

        public InitApiController(IWebHostEnvironment hostEnvironment,IGeoRepository geoRepository,IEmailService emailService) {
            this._hostingEnvironment = hostEnvironment;
            this._geoRepository = geoRepository;
            this._emailService = emailService;
        }

        [HttpGet, Route("geo"), AllowAnonymous]
        public async Task geo() {
            try
            {
                
                var path = _hostingEnvironment.WebRootPath + string.Format("{0}files{0}ok_geo.csv", Constants.DS);

                try
                {
                    System.Text.Encoding encoding = Encoding.Default;//GetType(filePath); //
                                                                     // DataTable dt = new DataTable();

                    System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read);

                    System.IO.StreamReader sr = new System.IO.StreamReader(fs, encoding);

                    //记录每次读取的一行记录
                    string strLine = "";
                    //记录每行记录中的各字段内容
                    string[] aryLine = null;
                    string[] tableHead = null;
                    //标示列数
                    int columnCount = 0;
                    //标示是否是读取的第一行
                    bool IsFirst = true;
                    List<Geo> data = new List<Geo>();
                    //逐行读取CSV中的数据
                    while ((strLine = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(strLine);
                        string[] cols = strLine.Replace("\"","").Split(',');
                        if (!IsFirst)
                        {
                            data.Add(new Geo
                            {
                                Id = int.Parse(cols[0]),
                                Pid = int.Parse(cols[1]),
                                Deep = int.Parse(cols[2]),
                                Name = cols[3],
                                ExtPath = cols[4],
                                Geos = cols[5],
                                Polygon = cols[6],
                            });
                        }
                        IsFirst = false;
                    }
                    sr.Close();
                    fs.Close();
                    await this._geoRepository.Save(data);
                }
                catch (Exception ex)
                {
                    throw Oops.Oh(ex.Message);
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }

        [HttpGet, Route("sendTestMail"),AllowAnonymous]
        public async Task<IActionResult> sendTestMail() {
           /* await this._emailService.Send(new EmailSendDto { 
                To ="690487807@qq.com",
                Title = "测试有间是否发送成功",
                Content = "测试吧!!!",
            });*/
            return new OkObjectResult(ResultUtil.Success());
        }

    }
}
